package com.xunyin.modules.mtopencity.rest;


import com.xunyin.api.XunyinException;
import com.xunyin.constant.Constants;
import com.xunyin.modules.mtopencity.domain.MtOpenCity;
import com.xunyin.modules.mtopencity.service.MtOpenCityService;
import com.xunyin.pagination.PaginationRequest;
import com.xunyin.pagination.PaginationResponse;
import com.xunyin.utils.CommonUtilV;
import com.xunyin.utils.StringUtil;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 开通城市管理
 * <p>
 * Created by xunyin
 * CopyRight 讯音云商 https://www.tonetn.com
 */
@Slf4j
@AllArgsConstructor
@Api(tags = "共享空间:开通城市管理")
@RestController
@RequestMapping("api/opencity")
public class BackendMtOpenCityController {

    /**
     * 开通城市接口
     */
    private final MtOpenCityService mtOpenCityService;

    /**
     * 开通城市列表查询
     *
     * @param request HttpServletRequest对象
     * @return 开通城市列表页面
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> list(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");
        Integer page = request.getParameter("page") == null ? Constants.PAGE_NUMBER : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("pageSize") == null ? Constants.PAGE_SIZE : Integer.parseInt(request.getParameter("pageSize"));
        String openStatus = request.getParameter("openStatus");
/*        String mobile = request.getParameter("mobile");
        String realName = request.getParameter("realName");
        String auditedStatus = request.getParameter("status");
        String storeId = request.getParameter("storeId");
        String category = request.getParameter("category");*/

        PaginationRequest paginationRequest = new PaginationRequest();
        paginationRequest.setCurrentPage(page);
        paginationRequest.setPageSize(pageSize);

        Map<String, Object> params = new HashMap<>();
/*        if (StringUtil.isNotEmpty(openStatus)) {
            params.put("openStatus", openStatus);
        }*/
/*        if (StringUtil.isNotEmpty(realName)) {
            params.put("name", realName);
        }
        if (StringUtil.isNotEmpty(mobile)) {
            params.put("mobile", mobile);
        }
        if (StringUtil.isNotEmpty(auditedStatus)) {
            params.put("status", auditedStatus);
        }
        if (StringUtil.isNotEmpty(storeId)) {
            if(Integer.parseInt(storeId)>0){
                params.put("storeId", storeId);
            }
        }
        if (StringUtil.isNotEmpty(category)) {
            params.put("category", category);
        }*/
        paginationRequest.setSearchParams(params);
        paginationRequest.setSortColumn(new String[]{"id desc"});
        PaginationResponse<MtOpenCity> paginationResponse = mtOpenCityService.queryListByPagination(paginationRequest);
        Map<String, Object> result = new HashMap<>();
        result.put("paginationResponse", paginationResponse);

        return new ResponseEntity<>(result, HttpStatus.OK);
        //return getSuccessResult(result);
    }


    /**
     * 保存开通城市信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseEntity saveHandler(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        String token = request.getHeader("Access-Token");
        String id = params.get("id") == null ? "0" : params.get("id").toString();
        String cityName = params.get("cityName") == null ? "0" : params.get("cityName").toString();
        String cityCode = params.get("cityCode") == null ? "0" : params.get("cityCode").toString();
        String openStatus = params.get("openStatus") == null ? "" : CommonUtilV.replaceXSS(params.get("openStatus").toString());
        String lng = params.get("lng") == null ? "" : CommonUtilV.replaceXSS(params.get("lng").toString());
        String lat = params.get("lat") == null ? "" : CommonUtilV.replaceXSS(params.get("lat").toString());

/*        AccountInfo accountInfo = TokenUtil.getAccountInfoByToken(token);
        if (accountInfo == null) {
            return getFailureResult(1001, "请先登录");
        }*/

        MtOpenCity mtOpenCity = new MtOpenCity();
        if (StringUtil.isNotEmpty(id)) {
            mtOpenCity = mtOpenCityService.queryMtOpenCityById(Integer.parseInt(id));
        }

        if (mtOpenCity == null && StringUtil.isNotEmpty(id)) {
            //return getFailureResult(201, "员工信息不存在");
            throw new XunyinException("开通城市信息不存在");
        }

        mtOpenCity.setCityName(cityName);
        mtOpenCity.setCityCode(cityCode);
        mtOpenCity.setOpenStatus(Integer.parseInt(openStatus));
        mtOpenCity.setLat(lat);
        mtOpenCity.setLng(lng);
        mtOpenCityService.saveMtOpenCity(mtOpenCity);
        return new ResponseEntity(HttpStatus.OK);
        // return getSuccessResult(true);
    }

    /**
     * 查询开通城市信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity<Map<String, Object>> getMtOpenCityInfo(HttpServletRequest request, @PathVariable("id") Integer id) {
        String token = request.getHeader("Access-Token");
        MtOpenCity mtOpenCity = mtOpenCityService.queryMtOpenCityById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("mtOpenCityInfo", mtOpenCity);
        return new ResponseEntity<>(result, HttpStatus.OK);
        //return getSuccessResult(result);
    }

    /**
     * 更新开通城市状态
     *
     * @return
     */
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @CrossOrigin
    public ResponseEntity updateStatus(HttpServletRequest request, @RequestBody Map<String, Object> params) {
        String token = request.getHeader("Access-Token");
        Integer status = params.get("status") != null ? Integer.parseInt(params.get("status").toString()) : 0;
        Integer id = params.get("id") == null ? 0 : Integer.parseInt(params.get("id").toString());
        mtOpenCityService.updateStatus(id, status);
        return new ResponseEntity(HttpStatus.OK);
        //return getSuccessResult(true);
    }


    /**
     * 删除开通城市信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseEntity deleteMtOpenCity(HttpServletRequest request, @PathVariable("id") Integer id) {
        String token = request.getHeader("Access-Token");
        //mtOpenCityService.updateStatus(id, 1);
        mtOpenCityService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
        // return getSuccessResult(true);
    }


}
